package com.jiangxiaofei.citycloud.base;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.activity.freshfruits.FreshFruitsGoodPublishActivity;
import com.jiangxiaofei.citycloud.activity.freshfruits.FreshFruitsNeedPublishActivity;
import com.jiangxiaofei.citycloud.activity.freshfruits.FreshFruitsSearchActivity;
import com.jiangxiaofei.citycloud.activity.user.LoginAcitivity;
import com.jiangxiaofei.citycloud.constants.Constants;

/**
 * @author: JiangXFei
 * @date: 2017/11/21 0021
 * @content: 设置供需交流模块BASEACTIVITY
 */

public abstract class HaveNeedBaseActivity extends BaseActivity {
    private ImageView imgBack;
    private TextView imgPublish;
    private LinearLayout linearSearch;
    private PopupWindow mPopupWindowPublish;
    private View popupView;


    public void initSearchTitle() {
        initPopupPublish();
        imgBack = findViewById(R.id.img_back);
        imgPublish = findViewById(R.id.img_publish);
        linearSearch = findViewById(R.id.linear_search);
        imgBack.setOnClickListener(v -> finish());
        imgPublish.setOnClickListener(v -> {
            if (EmptyUtils.isEmpty(Constants.userInfo)) {
                Toast.makeText(mContext, "请先登陆！", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, LoginAcitivity.class));
            } else {
                showPopup();
            }
        });
        linearSearch.setOnClickListener(v -> startActivity(new Intent(mActivity, FreshFruitsSearchActivity.class)));
    }

    /**
     * 初始化发布popupWindow
     */
    private void initPopupPublish() {
        mPopupWindowPublish = new PopupWindow(mContext);
        popupView = LayoutInflater.from(mContext).inflate(R.layout.popup_ios_buttom, null);
        showAnimation(popupView);
        mPopupWindowPublish.setContentView(popupView);
        mPopupWindowPublish.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindowPublish.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindowPublish.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopupWindowPublish.setFocusable(true);
        mPopupWindowPublish.setOutsideTouchable(false);
        TextView tvGong = popupView.findViewById(R.id.tv_publish_gong);
        TextView tcXu = popupView.findViewById(R.id.tv_publish_xu);
        TextView tvCannel = popupView.findViewById(R.id.tv_cannel);
        tvGong.setOnClickListener(v -> {
            mPopupWindowPublish.dismiss();
            startActivity(new Intent(mContext, FreshFruitsGoodPublishActivity.class));
        });
        tvCannel.setOnClickListener(v -> mPopupWindowPublish.dismiss());
        tcXu.setOnClickListener(v -> {
            mPopupWindowPublish.dismiss();
            startActivity(new Intent(mContext, FreshFruitsNeedPublishActivity.class));
        });
    }

    private void showPopup() {
        showAnimation(popupView);
        mPopupWindowPublish.showAtLocation(getCurrentFocus(), Gravity.BOTTOM, 0, 0); // 设置背景颜色变暗
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = 0.7f;
        mActivity.getWindow().setAttributes(lp);
        mPopupWindowPublish.setOnDismissListener(() -> {
            WindowManager.LayoutParams lp1 = mActivity.getWindow().getAttributes();
            lp1.alpha = 1f;
            mActivity.getWindow().setAttributes(lp1);
        });
    }

    private void showAnimation(View popView) {
        AnimationSet animationSet = new AnimationSet(false);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1.0f);
        alphaAnimation.setDuration(300);
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f
        );
        translateAnimation.setDuration(300);
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(translateAnimation);
        popView.startAnimation(animationSet);
    }

}
